package com.liuzhongni.wahahanews.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.adapter.RecyclerViewAdapterOfPicture;
import com.liuzhongni.wahahanews.adapter.RecyclerViewAdapterOfTopic;
import com.liuzhongni.wahahanews.singleton.StringRequestUtf;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;
import com.liuzhongni.wahahanews.topicclass.MyClick;
import com.liuzhongni.wahahanews.topicclass.TopicDetails;
import com.liuzhongni.wahahanews.topicclass.TopicFirst;

/**
 * Created by dllo on 16/1/7.
 */
public class TopicFragment extends Fragment implements MyClick{
    private RecyclerView recyclerView;
    private RecyclerViewAdapterOfTopic adapter;
    private TopicFirst topicFirst;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_topic, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.topic_rv);
        json();
        return view;
    }

    private void json() {
        String url = "http://c.3g.163.com/newstopic/list/expert/0-10.html";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("TopicFragment", response);
                Gson gson = new Gson();
                topicFirst = gson.fromJson(response, TopicFirst.class);
                init();
            }
        }, null);
        VolleySingleton.getOurInstance().getQueue().add(request);

    }

    private void init() {
        adapter = new RecyclerViewAdapterOfTopic(topicFirst.getData().getExpertList(), getActivity());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        adapter.setMyClick(this);
    }

    @Override
    public void itemClick(int position) {
        Intent intent = new Intent(getActivity(), TopicDetails.class);
        intent.putExtra("expertId",topicFirst.getData().getExpertList().get(position).getExpertId());
        intent.putExtra("position",position+"");
        startActivity(intent);
    }
}
